/**
 * 
 */
package com.ermax.aircond.util.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ermax.aircond.manage.domain.RealTimeControl;
import com.ermax.aircond.util.service.MachineCommandService;

/**
 * @author Eric
 *
 */
@Service("machineCommandService")
public class MachineCommandServiceImpl implements MachineCommandService {

	private static String COMMAND_PREFIX = "1";
	private static String SCHEDULE_PREFIX = "2";
	private static String EVENT_PREFIX = "3";
	
	private static String ON_OFF_TAG = "A";
	private static String ON_OFF_OPTION_ON = "1";
	private static String ON_OFF_OPTION_OFF = "0";
	
	private static String FAN_SPEED_TAG = "B";
	
	private static String DEFAULT_TEMPERATURE_TAG = "D";
	
	private static String VALVE_LEVEL_TAG = "V";

	
	@Override
	public String getTypeString(String type) {
		if (null == type) {
			return null;
		} else {
			return "T" + type;
		}
	}

	//on = 1
	//off = 0
	@Override
	public String getOnOffString(String onOff) {
		if (null == onOff) {
			return null;
		} else {
			return "A" + onOff;
		}
	}

	@Override
	public String getOnOffString(boolean onOff) {
		if (onOff) {
			return ON_OFF_TAG + ON_OFF_OPTION_ON;			
		} else {
			return ON_OFF_TAG + ON_OFF_OPTION_OFF;
		}
	}
	
	//0=AUTO,1=HIGH,2=MID,3=LOW
	@Override
	public String getFanSpeedString(String fanSpeed) {
		if (null == fanSpeed) {
			return null;
		} else {
			return FAN_SPEED_TAG + fanSpeed;
		}		
	}

	@Override
	public String getRoomTempString(int roomTemp) {
		// TODO Auto-generated method stub
		return "C" + roomTemp;
	}

	@Override
	public String getGateTempString(int gateTemp) {
		// TODO Auto-generated method stub
		return "G" + gateTemp;
	}

	//16~32
	@Override
	public String getDefaultTempString(String defaultTemp) {
		
		return DEFAULT_TEMPERATURE_TAG + defaultTemp;
	}

	@Override
	public String getValveOnOffString(int valueOnOff) {
		// TODO Auto-generated method stub
		return "F" + valueOnOff;
	}

	@Override
	public String getValveLevelString(String level) {
		// TODO Auto-generated method stub
		return VALVE_LEVEL_TAG + level;
	}

	@Override
	public String getMinTempString(int minTemp) {
		// TODO Auto-generated method stub
		return "X" + minTemp;
	}

	@Override
	public String getLockString(int lock) {
		// TODO Auto-generated method stub
		return "K" + lock;
	}

	@Override
	public String getStatusString(int status) {
		// TODO Auto-generated method stub
		return "O" + status;
	}

	@Override
	public String getFilterString(int filter) {
		// TODO Auto-generated method stub
		return "U" + filter;
	}

	@Override
	public String getErrorString(String error) {
		// TODO Auto-generated method stub
		return "E" + error;
	}

	@Override
	public int getNid(int gateNo, int deviceNo) {
		return (gateNo * 100) + deviceNo;
	}
	
	@Override
	public short getShortNid(int gateNo, int deviceNo) {
		Integer intNid = new Integer(this.getNid(gateNo, deviceNo));
		return intNid.shortValue();
	}

	@Override
	public String getCommandString(String command) {
		return COMMAND_PREFIX + command;
	}
	
	@Override
	public List<String> extractMachineData(String data) {
		List<String> results = new ArrayList<String>();
		if(data != null){
			char[] chars = data.toCharArray();
			StringBuilder sb = null;
			for(char c : chars){
				if(Character.isLetter(c)){
					if(sb == null){
						sb = new StringBuilder();
						sb.append(c);
					}else{
						results.add(sb.toString());
						sb = new StringBuilder();
						sb.append(c);
					}
				}else if(Character.isDigit(c)){
					if(sb != null){
						sb.append(c);
					}
				}				
			}
			if(sb != null){
				results.add(sb.toString());
			}
		}
		return results;
	}
	
	
	@Override
	public RealTimeControl translateCommand(RealTimeControl realTimeControl, String command) {
		String content = command.substring(1);
		
		//啟停		A 		A0停	A1啟
		if(command.startsWith(ON_OFF_TAG)){				
			if("1".equals(content)){
				realTimeControl.setStart(true);
			}else{
				realTimeControl.setStart(false);
			}
		}
		
		//風速		B 		B0自動 B1高 B2中 B3低
		if(command.startsWith(FAN_SPEED_TAG)){				
			realTimeControl.setFanSpeed(content);
		}
		
		//設定溫度	D		D16~D32
		if(command.startsWith(DEFAULT_TEMPERATURE_TAG)){
			realTimeControl.setTemperature(content);
		}
		
		//風門開度 	V 		V0~V100
		if(command.startsWith(VALVE_LEVEL_TAG)){
			realTimeControl.setWindValue(content);
		}
		
		return realTimeControl;
	}
	
	public static void main(String[] args){
		//String data = "A1B2C24D11E54K13Z21";
		String data = "123123A43";
		MachineCommandServiceImpl impl = new MachineCommandServiceImpl();
		List<String> list = impl.extractMachineData(data);
		for(String str : list){
			System.out.println(str);
		}
	}

}
